const express = require('express');
const multer = require('multer');
const path = require('path');
const router = express.Router();


const storage = multer.diskStorage({
	destination: function (req, file, cb) {
		cb(null, 'public/image/'); 
	},
	filename: function (req, file, cb) {
		const uniqueFilename = Date.now(); 
		const fileExtension = file.originalname.split('.').pop(); 
		cb(null, uniqueFilename + '.' + fileExtension); 
	}
});
const upload = multer({ storage: storage });
router.post('/upload', upload.single('file'), (req, res) => {
	if (req.file) {
		const imagePath = 'http://127.0.0.1:3000/image/' + req.file.filename;
		res.send({
			code: 200,
			message: 'success',
			result: imagePath
		});
	} else {
		res.send({
			code: 500,
			message: '上传失败！',
			result: null
		});
	}
});

module.exports = router;
